const express = require('express');
const bodyParser = require('body-parser');
const user = require('./user');
const fs = require('fs');

const app = express();

app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Methods', '*');
    res.setHeader('Access-Control-Allow-Origin', '*');
    next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use('/user', user);

app.listen(80, function () {
    console.log('启动成功');
});

function readFile (path, charset) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, charset, (err, data) => {
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        });
    });
}

(async () => {
    const data = await readFile('./package.json', 'utf8')
        .catch(err => {
            console.log('读取失败');
        });
        console.log(data);

})();
